package com.hd.crdao.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.LoanCarDetailBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.StringUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.wrapper.GlideImageUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/6/1 0001
 */
public class LoanCarInfoActivity extends BaseSecondActivity {


    @BindView(R.id.cardetail_head)
    ExpandHeadView mCardetailHead;
    @BindView(R.id.veBrand)
    TextView mVeBrand;
    @BindView(R.id.veFrame)
    TextView mVeFrame;
    @BindView(R.id.tranPlate)
    TextView mTranPlate;
    @BindView(R.id.veRegisterdate)
    TextView mVeRegisterdate;
    @BindView(R.id.veMileage)
    TextView mVeMileage;
    @BindView(R.id.veTransfertimes)
    TextView mVeTransfertimes;
    @BindView(R.id.vtDate)
    TextView mVtDate;
    @BindView(R.id.vcCredit)
    TextView mVcCredit;
    @BindView(R.id.vcRate)
    TextView mVcRate;
    @BindView(R.id.vcLstassess)
    TextView mVcLstassess;
    @BindView(R.id.vePledge)
    TextView mVePledge;
    @BindView(R.id.vehcileFrontImages)
    ImageView mVehcileFrontImages;
    @BindView(R.id.vehcileDriverImages)
    ImageView mVehcileDriverImages;
    @BindView(R.id.vehcileRegistrImages)
    ImageView mVehcileRegistrImages;
    @BindView(R.id.childset_layout)
    LinearLayout mChildLayout;
    @BindView(R.id.carcheck)
    Button mCarCheck;
    public LoanCarDetailBean mCarDetailBean;

    @Override
    protected int setContentView() {
        return R.layout.activity_loancarinfo;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mCardetailHead;
    }

    @Override
    protected void initView() {
        String loanCarState = getIntent().getStringExtra("loanCarState") + "";
        int userType = SPUtils.getInt(Config.userType, Config.CARBUSINESS_ACCOUNT);
        if (userType == Config.CHILD_ACCOUNT) {
            mChildLayout.setVisibility(View.GONE);
            mCardetailHead.getHeadViewRightText().setVisibility(View.GONE);
            if (TextUtils.equals(loanCarState , "2")) {
                mCarCheck.setVisibility(View.VISIBLE);
            } else {
                mCarCheck.setVisibility(View.GONE);//状态是已贷款状态才能进行盘点
            }
        } else {
            mChildLayout.setVisibility(View.VISIBLE);
            mCardetailHead.getHeadViewRightText().setVisibility(View.VISIBLE);
            mCarCheck.setVisibility(View.GONE);
        }
        initData();
        initEvent();
    }

    private void initEvent() {

        mCardetailHead.getHeadViewRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String vehCreditId = getIntent().getStringExtra("vehCreditId");
                String veBrand = getIntent().getStringExtra("veBrand");
                String veFrame = getIntent().getStringExtra("veFrame");

                Intent intent = new Intent(UIUtils.getContext(), ProgressActivity.class);
                intent.putExtra("vehCreditId", vehCreditId);
                intent.putExtra("veBrand", veBrand);
                intent.putExtra("veFrame", veFrame);
                startActivity(intent);

            }
        });
        mCarCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UIUtils.getContext(), TodayCarCheckActivity.class);
                intent.putExtra("vehicleId", SPUtils.getString("vehicleId", ""));
                startActivity(intent);
            }
        });

    }

    private void initData() {
        final String carId = getIntent().getStringExtra("carId") + "";
        OkHttpUtils.post().addParams("sign", Constants.SIGN)
                .addParams("data", carId).url(Constants.LOANCARINFO_URL).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {

                mCarDetailBean = new Gson().fromJson(response, LoanCarDetailBean.class);
                SPUtils.saveString("vehicleId", mCarDetailBean.getData().getId());
                if (mCarDetailBean.getSuccess() == 0) {
                    try {
                        setData(mCarDetailBean);  //设置数据
                    } catch (Exception e) {
                        ALog.d(e.getMessage());
                    }
                    MyToast.show(mCarDetailBean.getMessage());
                } else {
                    MyToast.show(mCarDetailBean.getMessage());
                }
            }
        });

    }


    public void setData(final LoanCarDetailBean mCarDetailBean) {
        mVeBrand.setText(mCarDetailBean.getData().getVeBrand());
        mVeFrame.setText(mCarDetailBean.getData().getVeFrame());
        mTranPlate.setText(mCarDetailBean.getData().getTranPlate());
        mVeRegisterdate.setText(StringUtils.getDate(mCarDetailBean.getData().getVeRegisterdate()));
        mVeMileage.setText(String.valueOf(mCarDetailBean.getData().getVeMileage()));
        mVeTransfertimes.setText(String.valueOf(mCarDetailBean.getData().getVeTransfertimes()));
        mVtDate.setText(mCarDetailBean.getData().getVehCredit().getVehTransfer().getVtDate());
        mVcCredit.setText(StringUtils.doubleToString(mCarDetailBean.getData().getVehCredit().getVcCredit()));
        mVcRate.setText(String.valueOf(mCarDetailBean.getData().getVehCredit().getVcRate()) + "%");
        mVcLstassess.setText(StringUtils.doubleToString(mCarDetailBean.getData().getVehCredit().getVcLstassess()));
        if (Integer.parseInt(mCarDetailBean.getData().getVePledge()) == 0) {
            mVePledge.setText("过户");
        } else {
            mVePledge.setText("抵押");
        }
         GlideImageUtils.loadImage(Constants.IMAGEURL + mCarDetailBean.getVehcileFrontImages().get(0).getThumbnailPath(),mVehcileFrontImages);
         GlideImageUtils.loadImage(Constants.IMAGEURL + mCarDetailBean.getVehcileDriverImages().get(0).getThumbnailPath(),mVehcileDriverImages);
         GlideImageUtils.loadImage(Constants.IMAGEURL + mCarDetailBean.getVehcileRegistrImages().get(0).getThumbnailPath(),mVehcileRegistrImages);

        mVehcileFrontImages.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String[] photoPaths = new String[]{mCarDetailBean.getVehcileFrontImages().get(0).getPsPath()};
                Intent intent = new Intent(UIUtils.getContext(), SwitchImageActivity.class);
                intent.putExtra("imagepath", photoPaths);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        initData();
        super.onResume();
    }


}
